package com.baosight.sczx.zp.service;

import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.FrameworkInfo;
import com.baosight.iplat4j.ep.QueryMap;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.iplat4j.security.bridge.SecurityBridgeFactory;
import com.baosight.platform.core.security.base.Filter;
import com.baosight.platform.core.security.base.GlobalConfig;
import com.baosight.platform.core.security.base.SecurityFactory;
import com.baosight.platform.core.security.base.SortCondition;
import com.baosight.platform.core.security.resourceset.IResourcesetManager;
import com.baosight.platform.core.security.resourceset.Resourceset;
import com.baosight.sczx.common.zp.ZPUtils;
import com.baosight.sczx.zp.domain.ZPResource;
import com.baosight.sczx.zp.domain.ZPResourceset;

/**
 * <p>Title: ServiceZP22.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 梁琦
 * @version 1.0
 * <pre>Histroy:
 *          2012-10-19 上午06:27:44  梁琦  Create
 *</pre>
 */
public class ServiceZP22 extends ServiceEPBase {
	
	private IResourcesetManager rManager = SecurityFactory.getInstance().getResourcesetManager();
	
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP22</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-19上午07:59:00 梁琦
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询资源组
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP22</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-19上午07:59:00 梁琦
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public EiInfo query(EiInfo inInfo) {	//queryResg

		QueryMap queryMap = new QueryMap();
		queryMap.loadEiInfo(inInfo, EiConstant.queryBlock, EiConstant.resultBlock);
		
		EiInfo outInfo = new EiInfo();
		outInfo.setBlock(inInfo.getBlock(EiConstant.queryBlock));
		outInfo.setAttr(inInfo.getAttr());
		EiBlock resultBlock = outInfo.addBlock(EiConstant.resultBlock);
		resultBlock.setBlockMeta(new ZPResourceset().eiMetadata);

		Filter filter = new Filter();
		if (queryMap.containsKey("Name")) {
			filter.put("Name", queryMap.get("Name"));
		}
		if(inInfo.getString("MODULE_ENAME") != null && (ZPUtils.USER_COMPANY_ID.equals(inInfo.getString("MODULE_ENAME"))==false)){
			filter.put("Attribute", inInfo.getString("MODULE_ENAME"));
		}
		
		SortCondition sort = new SortCondition();
		sort.addAsc("Name");

		List resourcesets = rManager.getResourcesets(filter, queryMap.getOffset(), queryMap.getLimit(), sort);
		resultBlock.setRows(resourcesets);
		
		int count = rManager.getResourcesetCount(filter);
		queryMap.setCount(count);
		queryMap.setEiInfo(outInfo, EiConstant.resultBlock);

		return outInfo;
	}

	/**
	 * 增添资源组
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP22</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-19上午07:59:00 梁琦
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo insert(EiInfo inInfo) {	//insertResg
		String regName = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "Name");
		String regDesc = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "DisplayName");
		String regAttr = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "Attribute");
		
		Resourceset set = new Resourceset();
		set.setDisplayName(regDesc);
		set.setName(regName);
		set.setAttribute(regAttr);
		set.setDesc(regDesc);
		rManager.createResourceset(set);

		if(inInfo.getBlock("ResResult")!= null){
			for (int i = 0; i < inInfo.getBlock("ResResult").getRowCount(); i++) {
				Map res = (Map) inInfo.getBlock("ResResult").getRow(i);

				String resName = (String) res.get("formEname");
				rManager.addMember(regName, resName, res.get("formType").toString());				
			}
		}

		if(inInfo.getString("roleId") != null && !"".equals(inInfo.getString("roleId"))){
			SecurityBridgeFactory.getBridge().grantAccess(inInfo.getString("roleId"), regName, GlobalConfig.TYPE_RESOURCESET);
		}
		
		return inInfo;
	}

	/**
	 * 修改资源组
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP22</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-19上午07:59:00 梁琦
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo update(EiInfo inInfo) {	//updateResg
		String regName = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "Name");
		String regDesc = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "DisplayName");
		Resourceset set = rManager.getResourceset(regName);
		set.setDisplayName(regDesc);
		set.setDesc(regDesc);

		rManager.updateResourceset(set);

		return inInfo;//queryResg
	}

	/**
	 * 删除资源组
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP22</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-18上午07:59:00 梁琦
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo delete(EiInfo inInfo) {	//deleteResg
		EiBlock rBlock = inInfo.getBlock(EiConstant.resultBlock);
		List regs = rBlock.getRows();

		for (int i = 0; i < regs.size(); i++) {
			Map reg = (Map) regs.get(i);
			if(reg.get("Name")!=null){
			rManager.deleteResourceset(reg.get("Name").toString());
			}
		}

		return query(inInfo);//queryResg
	}
	
	/**
	 * 新增资源组时查询资源
	 * <p>Function: queryRes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP22</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-19上午07:59:00 梁琦
	 * </p>
	 *
	 */
	public EiInfo queryRes(EiInfo inInfo) {
		inInfo.setCell(EiConstant.queryBlock, 0, "Projectprefix", FrameworkInfo.getProjectPrefix());
		inInfo.addBlock("ResResult").set("showCount", "true");
		inInfo.addBlock("ResResult").set("limit", "-1");
		EiInfo outInfo = super.query(inInfo, "ZPResource.query", new ZPResource(), false, null, EiConstant.queryBlock, "ResResult", "ResResult");
		return outInfo;
	}
}
